package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.mapper.AccountMapper;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public void init(LoginInfo loginInfo) {
        Account account = new Account();
        account.setId(loginInfo.getId());
        accountMapper.insert(account);

    }

    @Override
    public Account getById(Long id) {
        Account account = accountMapper.selectByPrimaryKey(id);
        //做判断
        if (!account.checkVerifyCode()) {
            throw new RuntimeException("账户:" + id + "资金数据异常,阻止业务继续进行");
        }
        return account;
    }

    @Override
    public void update(Account account) {
        Assert.isFalse(0 == accountMapper.updateByPrimaryKey(account), "账户信息修改失败,[乐观锁失败]");
    }
}
